<template>
  <div class="village">
    <el-row>
      <el-button icon="el-icon-plus" @click="adddialogVisible = true"
        >添加</el-button
      >
      <el-button icon="el-icon-delete" @click="dele">删除</el-button>
      <!-- <el-button icon="el-icon-download">导出文档</el-button> -->
    </el-row>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      :row-style="{ height: '48px' }"
      height="calc(100vh - 200px)"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleClick(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="Id" label="编号" width="150" />
      <!-- <el-table-column prop="Id" label="编码" width="150" /> -->
      <el-table-column prop="Village" label="村" width="150" />
      <el-table-column prop="Township" label="乡" width="150" />
    </el-table>
    <!-- 修改 -->
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :label-position="labelPosition" label-width="120px">
        <!-- <el-form-item label="编码">
          <el-input v-model="inputnum" placeholder="请输入编码" />
        </el-form-item> -->
        <el-form-item label="村名">
          <el-input v-model="inputcode" placeholder="请输入村名" />
        </el-form-item>
        <el-form-item label="乡名">
          <el-select
            v-model="inputcount"
            value-key="TownshipId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.Id"
              :label="item.Township"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear()">取 消</el-button>
        <el-button type="primary" @click="handleClose()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加 -->
    <el-dialog
      title="添加"
      :visible.sync="adddialogVisible"
      width="30%"
      :before-close="handleCloseadd"
    >
      <el-form :label-position="labelPosition" label-width="120px">
        <!-- <el-form-item label="编码">
          <el-input v-model="inputnum" placeholder="请输入编码" />
        </el-form-item> -->
        <el-form-item label="村名">
          <el-input v-model="inputcode" placeholder="请输入村名" />
        </el-form-item>
        <el-form-item label="乡名">
          <!-- <el-input v-model="inputcount" placeholder="请输入乡名" /> -->
          <el-select
            v-model="inputcount"
            value-key="TownshipId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.Id"
              :label="item.Township"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clear()">取 消</el-button>
        <el-button type="primary" @click="handleCloseadd()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { API_AREA_VILLAGE, API_AREA_TOWNSHIP } from "../../api/table";
import { API_VILL_ADD, API_VILL_DELE, API_VILL_CHGE } from "../../api/system";
export default {
  name: "",
  data() {
    return {
      msg: "村管理",
      labelPosition: "right",
      dialogVisible: false, // 修改模块显示隐藏
      adddialogVisible: false, // 添加模块显示隐藏
      tableData: [],
      /*
       *
       */
      inputnum: "",
      inputcode: "",
      inputcount: "",
      multipleSelection: [],
      tid: "",
      vid: "",
      options: [],
    };
  },
  methods: {
    // 删除
    dele() {
      let data = this.multipleSelection.map((item) => {
        return {
          Id: item.Id,
          Township: item.Township,
          TownshipId: item.TownshipId,
          Village: item.Village,
        };
      });
      if (data.length != 0) {
        API_VILL_DELE(data)
          .then((res) => {
            console.log(res);
            this.submit();
            this.$message.success("删除成功！");
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    handleClick(row) {
      this.inputcode = row.Village;
      this.inputcount = row.TownshipId;
      // this.tid = row.TownshipId;
      this.vid = row.Id;
      console.log(row);
      // row.Township;
      this.dialogVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleClose() {
      this.$confirm("确认修改？")
        .then((_) => {
          // done();
          let data = {
            ID: this.vid,
            townshipID: this.inputcount,
            village: this.inputcode,
          };
          API_VILL_CHGE(data)
            .then((res) => {
              console.log(res);
              this.submit();
              this.$message.success("修改成功!");
              this.clear();
              this.dialogVisible = false;
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch((_) => {});
    },
    handleCloseadd() {
      this.$confirm("确认增加？")
        .then((_) => {
          // done();
          let data = { village: this.inputcode, townshipID: this.inputcount };
          API_VILL_ADD(data)
            .then((res) => {
              console.log(res);
              this.adddialogVisible = false;
              this.submit();
              this.$message.success("添加成功!");
              this.clear();
            })
            .catch((err) => {
              console.log(err);
            });
          // this.adddialogVisible = false;
        })
        .catch((_) => {});
    },
    submit() {
      API_AREA_VILLAGE()
        .then((res) => {
          console.log(res);
          this.tableData = res.ResList;
        })
        .catch((err) => {
          console.log(err);
        });
      API_AREA_TOWNSHIP()
        .then((res) => {
          console.log(res);
          this.options = res.ResList;
          // this.tableData = res.ResList;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    clear() {
      this.adddialogVisible = false;
      this.dialogVisible = false;
      this.inputnum = "";
      this.inputcode = "";
      this.inputcount = "";
    },
  },
  created() {
    this.submit();
  },
};
</script>

<style scoped lang="scss">
.village {
  box-sizing: border-box;
  padding: 30px;
  // max-width: 716px;
  .el-row {
    text-align: left;
  }

  .el-table {
    margin-top: 20px;
  }
}
</style>
